/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.appspot.nnaplanner.DAL;

import java.io.Serializable;
import java.util.Date;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;

/**
 *
 * @author david
 */
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class DALGraph implements Serializable {

    @PrimaryKey
    @Persistent
    private String _id;

    @Persistent
    private Date _lastUpdated;

    @Persistent(serialized = "true", defaultFetchGroup="true")
    private SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> _graph;

    /**
     * Represents a graph. Currently we do not want users to create other graph pbjects
     * hence hiding the id fromt he client
     */
    public DALGraph() {
        _id = "gg1123123123";
        _lastUpdated = new Date(0);
        //_graph = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    }


    /**
     * This loads the graph from the GAE datastore, if it does not exist, then a
     * new graph is created
     * @return The current graph
     */
    public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> loadGraph() {
        try {
            DALGraph g = DAL.get().getPersistenceManager().getObjectById(DALGraph.class, _id );
            this._graph = g._graph;
            java.util.logging.Logger.getLogger(this.toString()).info("Loaded Graph");
            return g._graph;
        } catch (javax.jdo.JDOObjectNotFoundException e) {
            java.util.logging.Logger.getLogger(this.toString()).info("Created new Graph");
            return new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
        }

    }

    /**
     * Saves the Graph to the dataster
     */
    public void save() {
        save(this);
    }

    /**
     * Save the graph to the data store
     * @param graph the graph you want to save
     */
    public void save(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph){
        _graph = graph;
        save();
    }

    /**
     * Save the DALGraph object to the data store
     * @param dg The Graph you want o save
     * @return The id Of the saved graph
     */
    public String save(DALGraph dg) {
        DAL.save(dg);
        java.util.logging.Logger.getLogger(this.toString()).info("Saved Graph");
        return dg._id;

    }

    /**
     * Get the graph object form this DALGraph
     * @return The graph
     */
    public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> getGraph() {
        return _graph;
    }

    /**
     * Set the graph Object to the the parameter provided
     * @param graph The graph you want to set
     */
    public void setGraph(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph) {
        _graph = graph;
    }

    /**
     * @return The date this graph was last updated
     */
    public Date getlastUpdate() {
        return _lastUpdated;
    }

    /**
     * Set the last time this graph was updated
     * @param date the date this object was last updated
     */
    public void setlastUpdate(Date date) {
        _lastUpdated = date;
    }


}
